<?php

/**
 * Classe que cria um DataGrid padrão baseado no FlexiGrid
 */
class GFlexiGrid {

    var $_idGrid;
    var $_sql;
    var $_columns = array();
    var $_colModel = '';
    var $_sortName;
    var $_title;
    var $_post;
    var $_primaryKey;
    var $_isSearch = true;
    var $_fieldSearch;
    var $_isTableToggle = true;
    var $_width;
    var $_height;
    var $_minWidth; //min width of columns
    var $_minHeight; //min height of columns
    var $_useRp; //use the results per page select box
    var $_rp; // results per page
    var $_rpOptions; //option from per page select box
    var $_query;
    var $_qtype;
    var $_hideOnSubmit;
    var $_singleSelect = true;
    var $_order = 'asc';
    var $_grid;
    var $_actions;
    var $_actionsMass = 'delM:Excluir';
    var $_where;

    /**
     * Construtor da classe
     *
     * @param String $id
     * @param String $title
     * @param String $primaryKey
     * @param String $post

     */
    public function GFlexiGrid($idGrid, $title, $primaryKey, $post = 'grid.php') {
        $this->_idGrid = $idGrid;
        $this->_title = $title;
        $this->_post = $post;
        $this->_primaryKey = $primaryKey;
        //$sortName = split(',', $primaryKey);
        $this->_sortName = $primaryKey;
    }

    /**
     * Inserir uma nova coluna no Grid
     *
     * @param String $display Nome de exibição do campo
     * @param String $name Nome do campo no banco
     * @param String $width Largura da coluna em pixel
     * @param String $sortable Permite habilitar ordenação | Valores:['true','false'] | Default: false
     * @param String $align Alinhamento do valor na celula | Valores:['left','center','right'] | Default: left
     * @param String $visible Visibilidade da coluna | Valores:['true','false'] | Default: true
     * @param String $isSearch Se a coluna permite  | Valores:['true','false'] | Default: 'false'
     */
    public function setCol($display, $name, $width, $sortable = 'false', $align = 'left', $visible = 'true', $isSearch = 'false') {
        $this->_columns[] = array($display, $name, $width, $sortable, $align, $visible, $isSearch);
    }

    /**
     * Retornar todas as colunas que foram inseridas em forma de string
     */
    private function getColModel() {
        $count = count($this->_columns);
        for ($i = 0; $i < $count; $i++) {
            $this->_colModel .= '{';
            $this->_colModel .= " display: '" . $this->_columns[$i][0] . "'" .
                    ", name: '" . $this->_columns[$i][1] . "'" .
                    ", width : " . $this->_columns[$i][2] .
                    ", sortable : " . $this->_columns[$i][3] .
                    ", align: '" . $this->_columns[$i][4] . "'" .
                    ", visible: " . $this->_columns[$i][5] .
                    ", isSearch: '" . $this->_columns[$i][6] . "'";
            $this->_colModel .= ' }';

            if ($i < $count - 1) {
                $this->_colModel .= ',';
            }
        }
    }

    /**
     * Renderizar o grid, transforma todos os parâmetros para código JS
     */
    private function render() {
        if ($this->_actionsMass != '')
            array_unshift($this->_columns, array('', 'slc', 14, 'false', 'left', 'true', 'false'));
        else
            array_unshift($this->_columns, array('', 'slc', 14, 'false', 'left', 'false', 'false'));

        $this->getColModel();
        if ($this->_width != "")
            $this->_grid .= '<div class="flexigrid" style="width:' . $this->_width . 'px;float: left;">';
        else
            $this->_grid .= '<div class="flexigrid" style="width: 100%;float: left;">';

        $this->_grid .= '<div class="flexigridTitulo">' . $this->_title . '</div>';
        $this->_grid .= '<table id="' . $this->_idGrid . '"></table>';
        $this->_grid .= '<input type="hidden" id="__selecionados_' . $this->_idGrid . '" />';

        $this->_grid .= '<script>';
        $this->_grid .= $searchJs;
        $this->_grid .= 'jQuery("#' . $this->_idGrid . '").flexigrid({';
        $this->_grid .= 'url: "' . $this->_post . '",';
        $this->_grid .= 'colModel : [' . $this->_colModel . '],';

        $singleSelect = "true";
        if (!$this->_singleSelect) {
            $singleSelect = "false";
        }
        $this->_grid .= 'idGrid: "' . $this->_idGrid . '",';
        $this->_grid .= 'primaryKey: "' . $this->_primaryKey . '",';
        $this->_grid .= 'singleSelect: ' . $singleSelect . ',';
        $this->_grid .= 'sortname: "' . $this->_sortName . '",';
        $this->_grid .= 'sortorder: "' . $this->_order . '",';
        $this->_grid .= 'title: "' . $this->_title . '",';
        $rp = '10';

        if ($this->_rp != "") {
            $rp = $this->_rp;
        }
        $this->_grid .= 'rp: ' . $rp . ',';

        if ($this->_actions != "") {
            $this->_grid .= "actions: '" . $this->_actions . "',";
        }

        if ($this->_actionsMass != "") {
            $this->_grid .= "actionsMass: '" . $this->_actionsMass . "',";
        }

        if ($this->_where != "") {
            $this->_grid .= "where: '" . $this->_where . "',";
        }

        if ($this->_query != "" && $this->_qtype != "") {
            $this->_grid .= "qtype: '" . $this->_qtype . "',";
            $this->_grid .= "query: '" . $this->_query . "',";
        }

        $this->_grid .= 'showTableToggleBtn: "' . $this->_isTableToggle . '",';
        if ($this->_width != "")
            $this->_grid .= 'width: ' . $this->_width . ',';
        $this->_grid .= 'height: 228,';
        $this->_grid .= 'isSearch: "' . $this->_isSearch . '",';
        $this->_grid .= 'fieldSearch: "' . $this->_fieldSearch . '"';
        $this->_grid .= '});';
        $this->_grid .= '</script>';
        $this->_grid .= '</div>';
    }

    /**
     * Imprimir o Grid renderizado
     */
    public function show() {
        $this->render();
        echo $this->_grid;
    }

    /**
     * Retornar o Grid renderizado
     */
    public function get() {
        $this->render();
        return $this->_grid;
    }

    /**
     * Diz quais ações constarão no topo e em cada linha no modelo codigo:Título (ex: inserir:Inserir)
     * A primeira ação será para o topo do grid, as demais serão para cada linha
     *
     * @param String $actions [Default: 'editar,excluir']
     */
    function setActions($actions) {
        $this->_actions = $actions;
    }

    /**
     * Diz se é possível fazer buscas
     *
     * @param bool $isSearch [Default: true]
     */
    function setIsSearch($isSearch) {
        $this->_isSearch = $isSearch;
    }

   
    function setFieldSearch($fieldSearch) {
        $this->_fieldSearch = $fieldSearch;
    }

    function setColModel($colModel) {
        $this->_colModel = $colModel;
    }

    function setSortName($sortName) {
        $this->_sortName = $sortName;
    }

    function setWidth($width) {
        $this->_width = $width;
    }

    function setHeight($height) {
        $this->_height = $height;
    }

    function setMinWidth($minWidth) {
        $this->_minWidth = $minWidth;
    }

    function setMinHeight($minHeight) {
        $this->_minHeight = $minHeight;
    }

    function setUseRp($useRp) {
        $this->_useRp = $useRp;
    }

    function setRp($rp) {
        $this->_rp = $rp;
    }

    function setRpOptions($rpOptions) {
        $this->_rpOptions = $rpOptions;
    }

    function setQuery($query) {
        $this->_query = $query;
    }

    function setQtype($qtype) {
        $this->_qtype = $qtype;
    }

    function setHideOnSubmit($hideOnSubmit) {
        $this->_hideOnSubmit = $hideOnSubmit;
    }

    function setSingleSelect($singleSelect) {
        $this->_singleSelect = $singleSelect;
    }

    function setUrl($url) {
        $this->_url = $url;
    }

    function setSortorder($order) {
        $this->_order = $order;
    }

    function setActionsMass($actionsMass) {
        $this->_actionsMass = $actionsMass;
    }

    function setWhere($where) {
        $this->_where = $where;
    }

}

?>